﻿Imports System.Reflection
Imports System.Globalization

Public Class ssMain
    Dim a As New ExpansionModule
    Dim clsDBCon As New CoreCode.Connection

    Dim WithEvents btnX As DevComponents.DotNetBar.ButtonItem

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        End
    End Sub

    Private Sub ssMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TBConnect()
        MenuSetup()
        DisplayStatus()

        Timer1.Start()

    End Sub

    Public Function TBConnect() As Boolean
        With clsDBCon
            .DatabaseName = .SecDatabaseName
            .DatabasePassword = .ConnectionPassword
            .DatabaseServer = .ConnectionServer
            .DatabaseServerType = CoreCode.Connection.ServerType.MSSQL
            .DatabaseUserName = .ConnectionUserName
            .Connect(.SQLCn)
            .DatabaseName = .SetDatabaseName
            .Connect(.SQLCnSet)
            .DatabaseName = .TranDatabaseName
            .Connect(.SQLCnTran)
            .DatabaseName = .SecDatabaseName
            Return .Connect(.SQLCnSec)
        End With
    End Function

    Public Sub MenuSetup()
        Dim sDataSet As New DataSet
        With clsDBCon
            sDataSet.Tables.Clear()
            .ExecuteQuery("exec spStartMenuParent 'SUPERUSER'", sDataSet)
            For i As Integer = 0 To sDataSet.Tables(0).Rows.Count - 1
                btnX = New DevComponents.DotNetBar.ButtonItem
                btnX.ButtonStyle = DevComponents.DotNetBar.eButtonStyle.ImageAndText
                btnX.SubItemsExpandWidth = 24
                btnX.Name = sDataSet.Tables(0).Rows(i).Item("cModuleID")
                btnX.Text = sDataSet.Tables(0).Rows(i).Item("cModuleName")
                btnX.Tag = sDataSet.Tables(0).Rows(i).Item("cFormName")
                btnX.Image = .GetImage(sDataSet.Tables(0).Rows(i).Item("iIcon"))
                ItemContainer3.SubItems.Add(btnX)
                AddHandler btnX.Click, AddressOf CreateButton
            Next

            sDataSet.Tables.Clear()
            .ExecuteQuery("exec spStartMenuChild 'SUPERUSER'", sDataSet)
            For i As Integer = 0 To sDataSet.Tables(0).Rows.Count - 1
                btnX = New DevComponents.DotNetBar.ButtonItem
                btnX.ButtonStyle = DevComponents.DotNetBar.eButtonStyle.ImageAndText
                btnX.SubItemsExpandWidth = 24
                btnX.Name = sDataSet.Tables(0).Rows(i).Item("cModuleID")
                btnX.Text = sDataSet.Tables(0).Rows(i).Item("cModuleName")
                btnX.Tag = sDataSet.Tables(0).Rows(i).Item("cFormName")
                btnX.Image = .GetImage(sDataSet.Tables(0).Rows(i).Item("iIcon"))

                For Each obj As Object In ItemContainer3.SubItems
                    If sDataSet.Tables(0).Rows(i).Item("cParentID").ToString = obj.name Then
                        obj.SubItems.Add(btnX)
                    End If
                Next
                AddHandler btnX.Click, AddressOf CreateButton
            Next
        End With
    End Sub

    Private Sub CreateButton(ByVal sender As Object, ByVal e As System.EventArgs)
        If sender.tag = "" Then Exit Sub
        Dim sComponent As String = sender.tag
        Dim sCaption As String = sender.text
        Bar1.Visible = True
        a.CallComponent(Bar1, sComponent, sCaption)
    End Sub

    Private Sub DisplayStatus()
        With clsDBCon
            lblCompanyName.Text = .GetValueFrTable("cCompanyName", "sec_company", "cCompanyID='" & .CurrentCompany & "'")
            lblSoftwareName.Text = My.Application.Info.Title
            lblCurrentUser.Text = .CurrentFullName
            lblCurrentRole.Text = .CurrentRole
        End With
    End Sub

    Private Sub btnLogOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogOut.Click
        Me.Dispose()
        frmLogin.ClearData()
        frmLogin.Show()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblTime.Text = Now.ToString("F", CultureInfo.CreateSpecificCulture("en-US"))
    End Sub
End Class